package com.faithbj.device.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateUtils {
	
	private static Logger logger= LoggerFactory.getLogger(DateUtils.class);
	
	public static final long HOUR = 60L * 60L * 1000L;
	public static final long DAY = 24L * HOUR * 1000L;

	public static Date getAYearLater(String now) {
		String[] ss = now.split(" ");
		String[] ss1 = ss[1].split(":");	
		GregorianCalendar d = new GregorianCalendar();
		d.add(Calendar.YEAR,+1);
		d.set(Calendar.HOUR_OF_DAY, Integer.parseInt(ss1[0]));
		d.set(Calendar.MINUTE, Integer.parseInt(ss1[1]));
		d.set(Calendar.SECOND, Integer.parseInt(ss1[2]));
		return d.getTime();
	}
	
	/**
	 * 获取某天的第几个小时
	 * @param date
	 * @return
	 */
	public static int getHourOfDay(Date date) {
		GregorianCalendar d = new GregorianCalendar();
		d.setTime(date);
		return d.get(Calendar.HOUR_OF_DAY);
	}
	
	
	
	/**
	 * 获取几天的时间
	 * @param days 正数为几天后，反之为几天前
	 * @return
	 */
	public static Date getDaysAfter(int days) {
		GregorianCalendar d = new GregorianCalendar();
		d.add(Calendar.DATE, days);
		d.set(Calendar.HOUR_OF_DAY, 0);
		d.set(Calendar.MINUTE, 0);
		d.set(Calendar.SECOND, 0);
		d.set(Calendar.MILLISECOND, 0);
		return d.getTime();
	}
	/**
	 * 从某天(start)起，获取离start几天的时间
	 * @param days 正数为几天后，反之为几天前
	 * @return
	 */
	public static Date getDaysAfter(Date date,int days) {
		GregorianCalendar d = new GregorianCalendar();
		d.setTime(date);
		d.add(Calendar.DATE, days);
		d.set(Calendar.HOUR_OF_DAY, 0);
		d.set(Calendar.MINUTE, 0);
		d.set(Calendar.SECOND, 0);
		d.set(Calendar.MILLISECOND, 0);
		return d.getTime();
	}
	/**
	 * 以指定支付传格式，获取相对于现在，几天前的时间
	 * @param days 正数为几天后，反之为几天前
	 * @return
	 */
	public static String getDaysAfter(int days,String format) {
		GregorianCalendar d = new GregorianCalendar();
		d.add(Calendar.DATE, days);
		d.set(Calendar.HOUR_OF_DAY, 0);
		d.set(Calendar.MINUTE, 0);
		d.set(Calendar.SECOND, 0);
		d.set(Calendar.MILLISECOND, 0);
		return formateDate2String(format,d.getTime());
	}
	/**
	 * 以指定支付传格式，获取几天前的时间
	 * @param days 正数为几天后，反之为几天前
	 * @return
	 */
	public static Date getHoursAfter(Date date,int hours) {
		GregorianCalendar d = new GregorianCalendar();
		d.setTime(date);
		d.add(Calendar.DATE, 0);
		d.set(Calendar.HOUR_OF_DAY, hours);
		d.set(Calendar.MINUTE, 0);
		d.set(Calendar.SECOND, 0);
		d.set(Calendar.MILLISECOND, 0);
		return d.getTime();
	}
	
	
	
	/**
	 * 获得当前时间的开始时间
	 */  
	public static Date getCurrentBeginDate() {
		GregorianCalendar d = new GregorianCalendar();
		d.add(Calendar.DATE, 0);
		d.set(Calendar.HOUR_OF_DAY, 0);
		d.set(Calendar.MINUTE, 0);
		d.set(Calendar.SECOND, 0);
		d.set(Calendar.MILLISECOND, 0);
		return d.getTime();
	}
	
	public static Date getNextYear(Date now) {
		GregorianCalendar d = new GregorianCalendar();
		d.add(Calendar.YEAR,+1);
//		d.set(Calendar.HOUR_OF_DAY, Integer.parseInt(ss1[0]));
//		d.set(Calendar.MINUTE, Integer.parseInt(ss1[1]));
//		d.set(Calendar.SECOND, Integer.parseInt(ss1[2]));
		return d.getTime();
	}
	
	
	public static String formateDate2String(String format,Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		String dateString = simpleDateFormat.format(date);
		return dateString;
	}
	/**
	 * 获得当前时间字符串，如果默认不填写格式就是yyyy-MM-dd HH:mm:ss
	 * @param format 要返回的当前时间的格式串
	 */  
    public static String getCurrentDateString(String format){  
    	if(StringUtils.isBlank(format))
    		format="yyyy-MM-dd HH:mm:ss";
        Date currentTime = new Date();  
        SimpleDateFormat formatter = new SimpleDateFormat(format);  
        String dateString = formatter.format(currentTime);
        
        logger.debug(dateString);
        return dateString;  
    }  
	/**
	 * 把制定的时间按制定格式返回
	 * @param time 时间
	 * @param formatString 格式
	 * @return 日期型
	 */
	 public static Date getStringToDate(String time,String formatString){
		DateFormat format= new SimpleDateFormat(formatString);           
		Date d = null;  
		try {  
		        d = format.parse(time);  
		    } catch (Exception e) {  
		        e.printStackTrace();  
		}  
		return d;
	 }
	 /**
	  * 返回指定时间是星期几，星期日为1
	  * @param date
	  * @return
	  */
	 public static int getWeekOfDate(Date date){
		 Calendar calendar = Calendar.getInstance();
		 calendar.setTime(date);
		 int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);		 
		 return dayOfWeek;
	 }
	 
	 /**
	  * 返回两个日期相差几小时end-bein
	  * @param begin
	  * @param end
	  * @return
	  */
	 public static Integer betweenHours(Date begin,Date end){
		 Long between=end.getTime()-begin.getTime();
		 Long hours=between/HOUR;
		 return hours.intValue();
	 }
	 
	 
}
